package org.nulldevice.tankshot.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;

public class Wall extends Item {

	public static final int WALLPIECESIZEX = 30;
	public static final int WALLPIECESIZEY = 22;
	
	public static final int WALLPIECEOFFSETX = 15;
	public static final int WALLPIECEOFFSETY = 54;
	private int xparts= 0;
	private int yparts= 0;
	
	
	public Wall(int posX, int posY, int sizeX, int sizeY) {
		super(posX, posY, sizeX, sizeY, "", "wall.png", WALLPIECEOFFSETX, WALLPIECEOFFSETY);

		int x = sizeX;
		int y = sizeY;
		
		
		xparts = x/WALLPIECESIZEX;
		yparts = y/WALLPIECESIZEY;
		
		if (x%WALLPIECESIZEX!=0) xparts++;
		if (y%WALLPIECESIZEY!=0) yparts++;
		
		this.sizeX=xparts*WALLPIECESIZEX;
		this.sizeY=yparts*WALLPIECESIZEY;
		
		final int[] borderX = { posX - (this.sizeX / 2), posX - (this.sizeX / 2),
				 posX + (this.sizeX / 2), posX + (this.sizeX / 2) };
		final int[] borderY = { posY - (this.sizeY / 2), posY + (this.sizeY / 2),
				posY + (this.sizeY / 2), posY - (this.sizeY / 2) };
		border = new Polygon(borderX, borderY, borderX.length);
	}

	@Override
	public void draw(Graphics g) {
		super.draw(g);

		int x,y;
		int drawposX;
		int drawposY;
		
		for (x=0;x<xparts;x++){
			for (y=0;y<yparts;y++){
				drawposX=posX-sizeX/2+x*WALLPIECESIZEX+WALLPIECESIZEX/2;
				drawposY=posY-sizeY/2+y*WALLPIECESIZEY+WALLPIECESIZEY/2;
				imagerepresentant.draw(g, drawposX, drawposY, angle);
			}
		}
		
//		Color oldColor = g.getColor();
//		g.setColor(Color.gray);
//		g.fillRect(posX - (sizeX / 2), posY - (sizeY / 2), sizeX, sizeY);
//		g.setColor(oldColor);
	}

	public Polygon getBorder() {
		return border;
	}

	@Override
	public boolean move(double delta) {
		return false;
	}

	public int getDistanceToPoint(int px, int py) {

		int tx, ty;

		if (px < posX - (sizeX / 2)) { // left
			tx = posX - (sizeX / 2) - px;
		} else if (px > posX + (sizeX / 2)) { // right
			tx = px - posX - (sizeX / 2);
		} else
			tx = 0; // inside

		if (py < posY - (sizeY / 2)) { // above
			ty = posY - (sizeY / 2) - py;
		} else if (py > posY + (sizeY / 2)) { // below
			ty = py - posY - (sizeY / 2);
		} else
			ty = 0; // inside

		if (tx == 0)
			return ty;
		if (ty == 0)
			return tx;

		return (int) Math.sqrt((tx * tx) + (ty * ty));
	}

	@Override
	public void hitItem(Item item) {
		// It doesn't matter if the wall is hit

	}
}
